<?php
	include_once('htdu_core.php');
	class cart
	{
		private static $instance;
		private $product_array;
		private $code;
		
		private function __construct()
		{
			$product_array = array();
			$glo = new global_function();
			$this->code = $glo->create_ramdom_string(10);
		}
		
		public static function get_instance()
		{
			if(self::$instance == NULL)
			 	self::$instance = new self();
			return self::$instance;
		}
		
		public function add_product($product)
		{
			if($this->get_num_product() > 0)
			{
				foreach($this->product_array as $prod)
				{
					if($prod->get_id() == $product->get_id())
					{
						$prod->set_quantity($prod->get_quantity() + 1);
						return;
					}
				}
			}
			$this->product_array[] = $product;
		}
		
		public function update_product_qty($id, $qty)
		{
			foreach($this->product_array as $prod)
			{
				if($prod->get_id() == $id)
				{
					$prod->set_quantity($qty);
					return;
				}
			}
		}
		
		public function remove_product($id)
		{
			$found = false;
			for($i = 0; $i < sizeof($this->product_array); $i++)
			{
				if($id == $this->product_array[$i]->get_id())
				{
					$found = true;
					break;
				}
			}
			
			if($found){
				unset($this->product_array[$i]);
				$this->product_array = array_values($this->product_array);
			}
		}
		
		public function get_num_product()
		{	
			if(count($this->product_array) == 0)
				return 0;
			$qty = 0;
			foreach($this->product_array as $prod)
			{
				$qty += $prod->get_quantity();
			}
			return $qty;
		}
		
		public function get_total_money()
		{
			if($this->get_num_product() == 0)
				return 0;
			$money = 0;
			foreach($this->product_array as $product)
			{
				$money += $product->get_price();
			}
			return $money;
		}
		
		public function get_list_product()
		{
			return $this->product_array;
		}
		
		public function get_code()
		{
			return $this->code;
		}
		
		public function clear_cart()
		{
			$this->product_array = array();
			$glo = new global_function();
			$this->code = $glo->create_ramdom_string(10);
		}
	}
	
	class product
	{
		private $id;
		private $code;
		private $name;
		private $price;
		private $quantity;
		
		public function __construct($id, $code, $name, $price)
		{
			$this->code = $code;
			$this->id = $id;
			$this->name = $name;
			$this->price = $price;
			$this->quantity = 1;
		}
		
		public function get_id()
		{
			return $this->id;
		}
		
		public function get_code()
		{
			return $this->code;
		}
		
		public function get_name()
		{
			return $this->name;
		}
		
		public function get_price()
		{
			return $this->price;
		}
		
		public function get_money()
		{
			return $this->price * $this->quantity;
		}
		
		public function get_quantity()
		{
			return $this->quantity;
		}
		
		public function set_quantity($value)
		{
			$this->quantity = $value;
		}
		
		function __destruct()
		{
			$this->code;
			$this->id;
			$this->name;
			$this->price;
		}
	}
?>